﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using server.Common;

namespace server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpPost]
        public async Task<IActionResult> Post([FromFile]UserFile file)
        {
            if (file == null || !file.IsValid)
                return new JsonResult(new { code = 500, message = "不允许上传的文件类型" });

            string newFile = string.Empty;
            if (file != null)
                newFile = await file.SaveAs("wwwroot/images/");

            return new JsonResult(new { code = 0, message = "成功", url = newFile });
        }
        public async Task<IActionResult> Delete([FromBody]String[] name)
        {
          for (var i = 0;i<name.Length;i++){
          string image=name[i];            //图片地址
          string imgPath = "wwwroot/"+image;
          if (System.IO.File.Exists(imgPath))//判断文件是否存在
         {
            System.IO.File.Delete(imgPath);//执行IO文件删除图片,需引入命名空间System.IO;

          }
          }
          return new JsonResult(new { code = 0, message = "删除成功" });
        }
    }
}
